package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.dom4j.DocumentException;
import org.sccba.fps.common.CommonFunction;
import org.sccba.fps.enumeration.CommonEnum.FPSFieldType;
import org.sccba.fps.message.cnaps1.DetailStructureData;
import org.sccba.fps.message.cnaps1.StructureData;
import org.sccba.fps.message.cnaps1.structure.MessageBatchHeaderStructure;

import com.beps.beps121.CustomerCreditTransfer;
import com.beps.beps121.DocumentDocument;



public class XmlService implements MessageBatchHeaderStructure{

    private String filename = null;
    final static String m_namespaceDeclaration =
            "declare namespace ns='urn:cnaps:std:beps:2010:tech:xsd:beps.121.001.01';";

    public XmlService(String filename) {
        super();
        this.filename = filename;
    }
    
    public XmlService() {
        super();
    }

    public void customerReader() {
        try {
            File xmlFile = new File(filename);
            
            DocumentDocument doc = DocumentDocument.Factory.parse(xmlFile);
            CustomerCreditTransfer cstmrCdtTrf = doc.getDocument().getCstmrCdtTrf();
            String msgId = cstmrCdtTrf.getPKGGrpHdr().getMsgId();
            System.out.println(msgId);
            
            XmlOptions opt = new XmlOptions();
            opt.setUseDefaultNamespace();
            
            System.out.println(Integer.MAX_VALUE);
            String queryExpression = "$this//ns:Document//ns:CstmrCdtTrf//ns:PKGGrpHdr//ns:InstdPty//ns:InstdDrctPty";
            //Note that execQuery creates a new cursor
            XmlCursor rc = doc.newCursor();
            rc.selectPath(m_namespaceDeclaration+queryExpression);
            rc.toNextSelection();
            rc.getTextValue();
            XmlCursor resultCursor = doc.newCursor().execQuery(m_namespaceDeclaration+queryExpression,opt);
            System.out.println(resultCursor.currentTokenType()+"-"+resultCursor.xmlText()+"-"+resultCursor.getTextValue());
            
            String xmlQueryStatement = "$this//urn:Document//urn:CstmrCdtTrf//urn:CstmrCdtTrfInf";
            XmlObject[] oes = doc.selectPath(m_namespaceDeclaration+xmlQueryStatement,opt);
			for (XmlObject o : oes) {
				o.xmlText();
				XmlCursor xc = o.newCursor();
				xc.selectPath(m_namespaceDeclaration+"$this//urn:TxId");
				xc.toNextSelection();
				xc.getTextValue();
				
				XmlObject[] xos = o.selectPath(m_namespaceDeclaration+"$this//urn:TxId");
				xos[0].getDomNode().getNodeName();
				xos[0].getDomNode().getNodeValue();
     			System.out.println("aa");
			}
//            System.out.println(""+o.length);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("static-access")
	public static void main(String[] args) throws FileNotFoundException, DocumentException {
//        String filename = "E://beps.121.001.01.xml";
//        XmlService xmlService = new XmlService(filename);
//        xmlService.customerReader();
    	
//    	MsgReceiver receiver = new NPC2MsgReceiver();
//    	receiver.receive();
    	
    	
//    	ESBXmlBean xmlBean = new ESBXmlBean();
//    	ESBXmlHeader header = new ESBXmlHeader();
//    	header.setBranch_id("801123123");
//    	header.setChannel_id("14");
//    	xmlBean.setHeader(header);
//    	
//    	ESBXmlBody body = new ESBXmlBody();
//    	ESBXmlBodyExtAttr extAttr = new ESBXmlBodyExtAttr();
//    	body.setExtAttr(extAttr);
//    	
//    	SubTest subTest = new SubTest();
//    	
//    	body.setRequest(subTest);
//    	xmlBean.setBody(body);
//    	
//    	
//    	XStream xs = new XStream(new XppDriver(new XmlFriendlyReplacer("_-", "_")));
//    	xs.autodetectAnnotations(true);
//    	System.out.println(xs.toXML(xmlBean));
    	
//    	Npc1MsgReceiver receiver = new Npc1MsgReceiver();
//    	receiver.receive();
    	
//    	try {
//			Class clazz = Class.forName("org.sccba.fps.message.npc1.addition.ADD00103");
//			Method initMethod = clazz.getMethod("init", String.class);
//			Object o = clazz.newInstance();
//			if(o instanceof DetailStructureData){
//				CommonFunction.getObjectValue(o, "swiftNo");
//				List list = ((DetailStructureData) o).getDetails();
//				System.out.println("obj is a DetailStructureData.");
//			}else if(o instanceof StructureData){
//				System.out.println("obj is a StructureData.");
//			}
////			initMethod.invoke(o, "11111111111111111111111111111111111");
////			Object o2 = CommonFunction.invokeMethodWithSingleParam("org.sccba.fps.message.npc1.addition.ADD00103", "init", String.class, "11111111111111111111111111111");
//			
//			Method[] methods = clazz.getMethods();
//			for(Method m : methods){
//				System.out.println(m.getName());
//			}
//			System.out.println("----------------------------");
//			Method[] declaredMethods = clazz.getDeclaredMethods();
//			for(Method m : declaredMethods){
//				System.out.println(m.getName());
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    	
    	
        
    }

    
    
    
	@Override
	public void init(String str) {
		// TODO Auto-generated method stub
		
	}

}
